package com.zjmzxfzhl.modules.app.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjmzxfzhl.common.core.util.SecurityUtils;
import com.zjmzxfzhl.modules.app.entity.AppProd;
import com.zjmzxfzhl.modules.app.form.AppTaskVo;
import com.zjmzxfzhl.modules.app.service.AppTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppAlarm;
import com.zjmzxfzhl.modules.app.service.AppAlarmService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/alarm")
public class AppAlarmController extends BaseController {
    @Autowired
    private AppAlarmService appAlarmService;

    @Autowired
    private AppTaskService appTaskService;

    /**
     * 自定义查询列表
     * 
     * @param appAlarm
     * @param current
     * @param size
     * @return
     */
//    @PreAuthorize("@elp.single('app:alarm:list')")
    @GetMapping(value = "/list")
    public Result list(AppAlarm appAlarm, @RequestParam Integer current, @RequestParam Integer size) {
        String userId = SecurityUtils.getUserId();
        appAlarm.setUserIds(userId);
        IPage<AppAlarm> pageList = appAlarmService.list(new Page<AppAlarm>(current, size), appAlarm);
        return Result.ok(pageList);
    }

    @PreAuthorize("@elp.single('app:alarm:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppAlarm appAlarm = appAlarmService.getById(id);
        return Result.ok(appAlarm);
    }

    /**
     * @功能：新增
     * @param appAlarm
     * @return
     */
    @PreAuthorize("@elp.single('app:alarm:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppAlarm appAlarm) {
        appAlarmService.save(appAlarm);
        return Result.ok();
    }

    /**
     * @功能：修改
     * @param appAlarm
     * @return
     */
    @PreAuthorize("@elp.single('app:alarm:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppAlarm appAlarm) {
        appAlarmService.updateById(appAlarm);
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
    @PreAuthorize("@elp.single('app:alarm:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            appAlarmService.removeByIds(Arrays.asList(idsArr));
        } else {
            appAlarmService.removeById(idsArr[0]);
        }
        return Result.ok();
    }

    /**
     * @功能： 状态变更{00.待执行,01.已执行,02.已关闭}
     * @param alarmId
     * @param status
     * @return
     */
//    @PreAuthorize("@elp.single('app:task:update')")
    @PutMapping(value = "/alarmChange")
    public Result alarmChange(@RequestParam String alarmId,@RequestParam String status) {
        Boolean result =true;
        switch (status) {
            case "00": //待执行
                result =appAlarmService.wait(alarmId);
                break;
            case "01": //已执行
                result =appAlarmService.verified(alarmId);
                break;
            case "02": //已关闭
                result =appAlarmService.close(alarmId);
                break;
            default: return Result.error("查找事务状态失败！");
        }
        if(result==false){
            return Result.error("事务状态更改失败！");
        }
        return Result.ok();
    }

    /**
     * 自定义查询列表
     *
     * @return
     */
//    @PreAuthorize("@elp.single('app:alarm:list')")
    @GetMapping(value = "/getWaitNum")
    public Result getWaitNum() {
        Map<String,Object> result=new HashMap();
        AppAlarm appAlarm=new AppAlarm();
        appAlarm.setStatus("00");
        String userId = SecurityUtils.getUserId();
        appAlarm.setUserIds(userId);
        Integer alarmTotal = appAlarmService.selectRowCount(appAlarm);
        result.put("alarmTotal",alarmTotal);

        AppTaskVo appTaskVo=new AppTaskVo();
        appTaskVo.setStatus("00");
        appTaskVo.setMyTask(userId);
        appTaskVo.setProjUserId(userId);
        Integer taskTotal=appTaskService.selectRowCount(appTaskVo);
        result.put("taskTotal",taskTotal);
        return Result.ok(result);
    }

}
